<template>
  <div class="JADP-common-layout">
    <div class="JADP-common-layout-center">
      <el-row class="JADP-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item label="评价分类">
              <el-select
                v-model="query.categore"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="(item, index) in getDict('pingjiafenlei')"
                  :key="index"
                  :label="item.dataName"
                  :value="item.id"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="评价内容">
              <el-input v-model="query.content" placeholder="请输入" clearable>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()"
                >查询</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >重置</el-button
              >
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JADP-common-layout-main JADP-flex-main">
        <div class="JADP-common-head">
          <div>
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="addOrUpdateHandle()"
              >新增
            </el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              @click="handleBatchRemoveDel()"
              >批量删除
            </el-button>
          </div>
          <div class="JADP-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh JADP-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
            <screenfull :isContainer="true" />
            <el-tooltip effect="dark" content="列设置" placement="top">
              <el-link
                icon="icon-ym icon-ym-options JADP-common-head-icon"
                :underline="false"
              />
            </el-tooltip>
          </div>
        </div>
        <el-table
          v-loading="listLoading"
          :data="list"
          ref="table"
          border
          class="JADP-common-table"
          @selection-change="handleSelectionChange"
          height="100%"
        >
          <el-table-column
            type="selection"
            width="55"
            :selectable="selectable"
          />
          <el-table-column type="index" label="序号" width="50" />
          <el-table-column prop="categore" label="评价分类" align="left">
            <template v-slot="{ row }">
              {{ getDict("pingjiafenlei", row.categore) }}
            </template>
          </el-table-column>
          <el-table-column prop="content" label="评价内容" align="left" />
          <el-table-column prop="method" label="评分方式" align="left">
            <template v-slot="{ row }">
              {{ row.method == 1 ? "分值" : "自定义" }}
            </template>
          </el-table-column>
          <!-- <el-table-column prop="categore" label="评价分类" align="left">
            <template v-slot="{ row }">
              {{ getDict("pingjiafeilei", row.categore) }}
            </template>
          </el-table-column> -->
          <!-- <el-table-column prop="managename" label="评价名称" align="left" /> -->
          <!-- <el-table-column prop="releasestatus" label="发布状态" align="left">
            <template v-slot="{ row }">
              {{ row.releasestatus == 1 ? "已发布" : "未发布" }}
            </template>
          </el-table-column> -->
          <el-table-column prop="creatoruserid" label="创建用户" align="left" />
          <el-table-column prop="creatortime" label="创建时间" align="left" />

          <el-table-column label="操作" fixed="right" width="180">
            <template slot-scope="scope">
              <el-button type="text" @click="addOrUpdateHandle(scope.row.id)"
                >编辑
              </el-button>
              <el-button
                v-if="scope.row.releasestatus == 1"
                type="text"
                class="JADP-table-delBtn"
                @click="handleDelT"
                >删除
              </el-button>
              <el-button
                v-else
                type="text"
                class="JADP-table-delBtn"
                @click="handleDel(scope.row.id)"
                >删除
              </el-button>

              <el-button
                type="text"
                @click="addOrUpdateHandle(scope.row.id, true)"
                >详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- -------------------------------------- -->

        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
    <JADP-Form v-if="formVisible" ref="JADPForm" @refresh="refresh" />
    <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
    <ImportBox
      v-if="importBoxVisible"
      ref="importBox"
      @refresh="reset()"
      @ImportPreviewApi="ImportPreviewApi"
      @importDataApi="importDataApi"
      @TemplateDownload="TemplateDownload"
      :URL="URL"
    />
    <Score v-if="scoreVisible" ref="scoreForm" @refresh="refresh" />
  </div>
</template>

<script>
// import { mapGetters } from "vuex";
import request from "@/utils/request";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
import JADPForm from "./Form";
import ExportBox from "./ExportBox";
import Score from "./score";
export default {
  name: "evaluationManagement",
  components: { JADPForm, ExportBox, Score },
  data() {
    return {
      //导入属性
      importBoxVisible: false,
      URL: "xy_pj_manage/Xy_pj_manage",
      query: {
        categore: undefined,
        content: undefined,
      },
      defaultProps: {
        children: "children",
        label: "fullName",
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
      },
      formVisible: false,
      exportBoxVisible: false,
      scoreVisible: false,
      columnList: [
        { prop: "categore", label: "评价分类" },
        { prop: "managename", label: "评价名称" },
        { prop: "creatoruserid", label: "创建用户" },
        { prop: "creatortime", label: "创建时间" },
      ],
    };
  },
  computed: {
    // ...mapGetters(["userInfo"])
  },
  created() {
    this.initData();
  },
  methods: {
    // row.releasestatus == 1 ? "已发布" : "未发布"
    selectable(row) {
      if (row.releasestatus == 1) {
        return false;
      } else {
        return true;
      }
    },
    ImportPreviewApi(val) {
      let query = { fileName: val };
      request({
        url: `/api/xy_pj_manage/Xy_pj_manage/ImportPreview`,
        method: "get",
        data: query,
      })
        .then((res) => {
          this.$refs.importBox.list = res.data.dataRow;
          this.$refs.importBox.btnLoading = false;
          this.$refs.importBox.active++;
        })
        .catch(() => {
          this.$refs.importBox.btnLoading = false;
        });
    },
    importDataApi(val) {
      let query = { list: val ,organizeId :this.$store.getters.organizeId};
      request({
        url: `/api/xy_pj_manage/Xy_pj_manage/ImportData`,
        method: "post",
        data: query,
      })
        .then((res) => {
          this.$refs.importBox.result = res.data;
          this.$refs.importBox.resultList = res.data.failResult;
          this.$refs.importBox.btnLoading = false;
          this.$refs.importBox.active++;
        })
        .catch(() => {
          this.$refs.importBox.btnLoading = false;
        });
    },
    TemplateDownload() {
      request({
        url: `/api/xy_pj_manage/Xy_pj_manage/templateDownload?organizeId=${this.$store.getters.organizeId}`,
        method: "post",
      }).then((res) => {
        if (!res.data.url) return;
        window.location.href = this.define.comUrl + res.data.url;
      });
    },
    uploadForm() {
      this.importBoxVisible = true;
      this.$nextTick(() => {
        this.$refs.importBox.init(this.columnList);
      });
    },
    initData() {
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
        organizeId:this.$store.getters.organizeId
      };
      let query = {};
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join();
        } else {
          query[key] = _query[key];
        }
      }
      request({
        url: `/api/xy_pj_manage/Xy_pj_manage`,
        method: "get",
        data: query,
      }).then((res) => {
        this.list = res.data.list;
        this.total = res.data.pagination.total;
        this.listLoading = false;
      });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          request({
            url: `/api/xy_pj_manage/Xy_pj_manage/${id}`,
            method: "DELETE",
          }).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    handleDelT() {
      this.$message({
        message: "该评价状态已发布，不可删除",
        type: "error",
        duration: 1500,
      });
    },
    handleSelectionChange(val) {
      const res = val.map((item) => item.id);
      this.multipleSelection = res;
    },
    handleBatchRemoveDel() {
      if (!this.multipleSelection.length) {
        this.$message({
          type: "error",
          message: "请选择一条数据",
          duration: 1500,
        });
        return;
      }
      const ids = this.multipleSelection.join();
      this.$confirm("您确定要删除这些数据吗, 是否继续？", "提示", {
        type: "warning",
      })
        .then(() => {
          request({
            url: `/api/xy_pj_manage/Xy_pj_manage/batchRemove/${ids}`,
            method: "DELETE",
          }).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.JADPForm.init(id, isDetail);
      });
    },
    exportData() {
      this.exportBoxVisible = true;
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList);
      });
    },
    download(data) {
      let query = { ...data, ...this.listQuery, ...this.query };
      request({
        url: `/api/xy_pj_manage/Xy_pj_manage/Actions/Export`,
        method: "GET",
        data: query,
      }).then((res) => {
        if (!res.data.url) return;
        window.location.href = this.define.comUrl + res.data.url;
        this.$refs.ExportBox.visible = false;
        this.exportBoxVisible = false;
      });
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
      };
      this.initData();
    },
    refresh(isrRefresh) {
      this.formVisible = false;
      this.scoreVisible = false;
      if (isrRefresh) this.reset();
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
      };
      this.initData();
    },
    // edit(id, categore) {
    //   this.scoreVisible = true;
    //   this.$nextTick(() => {
    //     this.$refs.scoreForm.init(id, categore);
    //   });
    // },
  },
};
</script>
